<?php
namespace App\Http\Controllers\Admin;
use App\http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class ClassifyController extends BaseController{
    //商品分类列表页面
    public function classify_list(){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $classify_p = DB::table("classify")->orderBy("classify_addtime","DESC")->paginate(5);
        // dd($classify_p);die;
        $count = $classify_p->total();
        $classify = $classify_p->items();
        foreach ($classify as $key => $value) {
            $classify[$key]->classify_addtime = date("Y-m-d",$value->classify_addtime);
        }
        return view('admin/classify/classify_list',["classify"=>$classify,"classify_p"=>$classify_p,"count"=>$count]);
    }
    //获取商品分类某一页的ajax方法
    public function toget_classify_list(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $classify_p = DB::table("classify")
        ->orderBy("classify_addtime","DESC");
        if($request->classify_name){
            $classify_p = $classify_p->where("classify_name","like","%".$request->classify_name."%");
        }
        if($request->start){
            $classify_p = $classify_p->where("classify_addtime",">=",strtotime($request->start));
        }
        if($request->end){
            $classify_p = $classify_p->where("classify_addtime","<=",strtotime($request->end));
        }
        $classify_p = $classify_p->paginate(5);
        // ->paginate(5);
        // dd($classify);
        // echo $classify_p->render();
        // dd($request->op);
        // die();
		$classify = $classify_p->items();
        $current = $classify_p->currentPage();
        echo '
        <div class="layui-form" >
        <table class="layui-table">
            <thead>
            <tr>
                <th>
                <input type="checkbox" name="all" value="" lay-skin="primary" lay-filter="all" />
                </th>
                <th>分类名称</th>
                <th>分类排序</th>
                <th>添加时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
        ';
        // print_r($classify);
        foreach ($classify as  $value) {
            $classify_addtime = date("Y-m-d",$value->classify_addtime);
            $classify_id = $value->classify_id;
            echo '
            <tr>
                <td>
                <input type="checkbox" name="id[]" value="'.$classify_id.'" lay-skin="primary" lay-filter="item" />
                </td>
                <td>'.$value->classify_name.'</td>
                <td>'.$value->classify_ord.'</td>
                <td>'.$classify_addtime.'</td>
                <td class="td-manage">
                <a title="编辑"  onclick="x_admin_show('."'编辑'".','."'/admin/edit_classify?classify_id=$classify_id'".')" href="javascript:;">
                    <i class="layui-icon">&#xe63c;</i>
                </a>
                <a title="删除" onclick="member_del(this,'.$classify_id.')" href="javascript:;">
                    <i class="layui-icon">&#xe640;</i>
                </a>
                </td>
            </tr>
            ';
        }
        echo '
        </tbody>
        </table>
        </div>
        <div class="page">'.$classify_p->render().'</div>
        ';
    }
    //获取商品分类总数
    public function toget_classify_count(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $classify = DB::table('classify');
        if($request->classify_name){
            $classify = $classify->where("classify_name","like","%".$request->classify_name."%");
        }
        if($request->start){
            $classify = $classify->where("classify_addtime",">=",strtotime($request->start));
        }
        if($request->end){
            $classify = $classify->where("classify_addtime","<=",strtotime($request->end));
        }
        $classify = $classify->count();
        // ->count();
        echo $classify;
    }
    //商品分类添加页面
    public function add_classify(){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        return view("admin/classify/classify_add");
    }
    //商品分类添加异步
    public function todo_addclassify(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $data = array(
            "classify_name"=>$request->classify_name,
            "classify_addtime"=>time(),
            "classify_ord"=>$request->classify_ord
        );
        $res = DB::table("classify")->insert($data);
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"添加成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
    }
    //商品分类编辑页面
    public function edit_classify(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $classify = DB::table("classify")->where("classify_id",$request->classify_id)->first();
        return view("admin/classify/classify_edit",["classify"=>$classify]);
    }
    //商品分类编辑异步
    public function todo_editclassify(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $data = array(
            "classify_name"=>$request->classify_name,
            "classify_ord"=>$request->classify_ord
        );
        $res = DB::table("classify")->where("classify_id",$request->classify_id)->update($data);
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"保存成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
    }
    //商品分类删除
    public function todo_delclassify(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $classify_id = $request->classify_id;
        $res = DB::table("classify")->where("classify_id",$classify_id)->delete();
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"删除成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
    }
    //批量删除
    public function todo_delclassifys(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        // print_r($request->all());
        $idarr = $request->id;
        $res = DB::table("classify")->whereIn("classify_id",$idarr)->delete();
        if ($res) {
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"删除成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据库执行有误"
            ));die;
        }
    }
}